<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        :loadOnCreated="false"
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            充值
          </el-button>
        </template>

        <template #status="{ row }">
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>

        <template #amount="{ row }">
          <span class="pointer blue" @click="openDetail(row)">
            {{ row.amount }}
          </span>
        </template>

        <template #tax="{ row }"> {{ row.tax }}% </template>

        <template #images="{ row }">
          <span class="pointer blue" @click="openImages(row)"> 查看 </span>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="danger" size="small" @click="del(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
    <Images v-model="showImages" :data="current" />
    <Detail v-model="showDetail" :tenant-id="currentId" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './search.vue';
  import Edit from './edit.vue';
  import Images from './images.vue';
  import Detail from '@/views/system/tenant/components/balance-detail.vue';
  import { pageTenantPayRecord, delTenantPayRecord } from '@/api/company';
  import { Plus } from '@element-plus/icons-vue';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];
    cols.push(
      {
        prop: 'id',
        label: 'ID',
        width: 80,
        align: 'center'
      },
      // {
      //   slot: 'status',
      //   label: '状态',
      //   width: 90,
      //   align: 'center'
      // },
      {
        prop: 'tenant',
        label: '所属劳务'
      },
      {
        prop: 'payTime',
        label: '付款时间',
        width: 165
      },
      {
        prop: 'final_amount',
        label: '付款金额',
        width: 110
      },
      {
        slot: 'tax',
        label: '服务费',
        width: 90
      },
      {
        slot: 'amount',
        label: '到账金额',
        width: 110
      },

      {
        slot: 'images',
        label: '相关凭证',
        width: 90,
        align: 'center'
      },
      {
        prop: 'remark',
        label: '备注',
        width: 200
      },
      {
        prop: 'create_user',
        label: '提交人',
        width: 80
      },
      {
        prop: 'create_time',
        label: '提交时间',
        width: 140
      }
    );
    // if (status.value == 200) {
    //   cols.push(
    //     {
    //       prop: 'judge_user',
    //       label: '审核',
    //       width: 80
    //     },
    //     {
    //       prop: 'judge_time',
    //       label: '审核时间',
    //       width: 140
    //     }
    //   );
    // }

    cols.push({
      columnKey: 'action',
      label: '操作',
      width: 130,
      slot: 'action',
      align: 'center',
      fixed: 'right'
    });

    return cols;
  });

  /** 删除 */
  const del = (row) => {
    ElMessageBox.confirm('删除此记录2', '系统提示', {
      type: 'warning',
      draggable: true
    })
      .then(() => {
        const loading = EleMessage.loading({
          message: '正在处理中..',
          mask: true
        });
        delTenantPayRecord({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功!');
            reload();
          })
          .catch(() => {
            loading.close();
          });
      })
      .catch(() => {});
  };

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageTenantPayRecord({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  const showImages = ref(false);
  const openImages = (row) => {
    showImages.value = true;
    current.value = row;
  };
  const currentId = ref(null);
  const showDetail = ref(false);
  const openDetail = (row) => {
    currentId.value = row.tenant_id;
    showDetail.value = true;
    current.value = row;
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };
</script>
